/**
 * Created by Administrator on 2016/8/25.
 */



define(['common/controllers', 'domReady'],
    function (controllers, domReady) {
        controllers.controller('rPacketCtrl',function ($scope,$location,$state,$cacheFactory,rPacketService,validation) {
            $scope.params = {
                size:5,
                page:1
            }
            $scope.currentPage = 1;
            $scope.point=[];
            $scope.pages = [];
            $scope.rPacketType = [{typeId:1,typeName:'平分红包'},{typeId:2,typeName:'运气红包'},{typeId:3,typeName:'任务红包'}];

            //查询所有state=1，status=2的红包任务
            rPacketService.getRPTask(function(err,data){
                if(err){
                    console.log(err);
                    return err;
                }
                $scope.rPTask = data;
                console.log($scope.rPTask);
            })

            var load = function(params){
                params.page =$scope.currentPage;
                rPacketService.getSysRPList(params,function(err,data){
                    if(err){
                        return err;
                    }
                    data.results.forEach(function(item){
                        if(item.red_packet_type == 1){
                            item.type_name = '平分红包';
                        }else if(item.red_packet_type == 2){
                            item.type_name = '运气红包';
                        }else{
                            item.type_name = '任务红包';
                        }
                    })
                    $scope.sysRPList = data.results;
                    $scope.count = data.counts;
                    //分页后的订单
                    $scope.numPages = data.counts>0?Math.ceil(data.counts / params.size):1;
                    $scope.pageStart = data.counts>0?($scope.currentPage - 1) * params.size + 1:0;
                    $scope.pageEnd = params.size * $scope.currentPage > data.counts ? data.counts : $scope.currentPage * params.size;
                })
            }

            // 翻页
            $scope.onSelectPage = function (page) {
                $scope.currentPage = page;
                //$scope.params.page = page;

                load($scope.params);
            };

            $scope.judge=function(page,index){
                if(Math.abs($scope.currentPage - page)==5&&page!=1&&page!=$scope.numPages){
                    $scope.point[index]=true;
                }else{
                    $scope.point[index]=false;
                }

                if(Math.abs($scope.currentPage - page)<=5||page==$scope.numPages||page==1){
                    return true;
                }else{
                    return false;
                }
            }

            load($scope.params);

            $scope.addRPacket = function(){
                $scope.rPacket = {
                    roomId:0,
                    rPacketType:1,
                    taskId:0,
                    totalMoney:0,
                    totalQuantity:0,
                    timeInterval:60
                }

                $('#addRPacketModal').modal();
            }

            $scope.selectType = function(){
                if($scope.rPTask.length>0&&$scope.rPacket.rPacketType==3){
                    $scope.rPacket.taskId = $scope.rPTask[0].task_id;
                }else if($scope.rPTask.length==0&&$scope.rPacket.rPacketType==3){
                    alert('没有任务红包任务!!!');
                    $scope.rPacket.rPacketType = 1;
                    $scope.rPacket.taskId = 0;
                }else{
                    $scope.rPacket.taskId = 0;
                }
            }


            $scope.createRPacket = function(){
                console.log($scope.rPacket);
                rPacketService.createSysRPacket($scope.rPacket,function(err,data){
                    if(err){
                        return err;
                    }
                    location.reload();
                })
            }

            $scope.changeSysRPStatus = function(sysRPacketId,status){
                rPacketService.changeSysRPStatus(sysRPacketId,status,function(err,data){
                    if(err){
                        return err;
                    }
                    load($scope.params);
                })
            }

            $scope.delete = function(sysRPacketId){
                rPacketService.changeSysRPState(sysRPacketId,function(err,data){
                    if(err){
                        return err;
                    }
                    load($scope.params);
                })
            },

            $scope.editor = function(sysRPacketId){
                //根据ID查询当前任务
                rPacketService.getSysRPDetail(sysRPacketId,function(err,data){
                    if(err){
                        return err;
                    }
                    $scope.rPacket = data;
                })
                $('#updateRPacketModal').modal();
            }

            //确认修改红包,暂时只修改总任务信息
            $scope.updateRPacket = function(rPacket){
                rPacketService.updateRPacket(rPacket,function(err,data){
                    if(err){
                        alert('修改任务信息失败');
                        location.reload();
                    }

                    location.reload();
                })
            }






        })

    });

